<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/html/js/axios.min.js"></script>
    <script src="/html/js/ajax.js"></script>
    <script src="/html/js/layui.js"></script>
    <script src="/html/js/util.js"></script>
    <link rel="stylesheet" href="/html/js/css/layui.css" media="all">
    <title>经理管理商品</title>
</head>

<script>
    let productId;
    window.onload = function () {
        productId = getQuery("productId");
        showProduct();
    }


    async function showProduct() {
        let param = {productId: productId};
        let p = await ajaxGet("/product/findById", param);
        console.log(p);
        let str = "";
        str += "<div style='display: flex;justify-content: center'>"
        str += "<div>";
        str += `<div style="margin: 20px">产品名：${p.productName}</div>`;
        str += `<img style="height: 400px" src='/html/imgs/${p.productImg}'>`;
        str += `<div style="margin: 20px">产品价格：<input id="productPrice" type="text" onblur="updateProductPrice()" value="${p.productPrice}"></div>`;
        str += `<div style="margin: 20px">已售出：${p.soldNum} 件</div>`;
        str += `<div style="margin: 20px">产地：${p.productLocation}</div>`;
        str += `<div style="margin: 20px">生产日期：${p.productDate}</div>`;
        str += `商品状态：<div id="productStatue" style="margin: 20px">${p.productStatue}</div>`;
        str += `<button type="button" class="layui-btn"><a href="/html/owner/ownerShow.html">返回</a></button>`;
        str += `<button type="button" class="layui-btn" onclick="updateProductStatue()">点击修改状态</button>`;
        str += "</div>";
        $("productContent").innerHTML = str;
    }

    //更新商品状态
    async function updateProductStatue() {
        let productStatue = $("productStatue").innerHTML;

        if (productStatue == "上架") {
            productStatue = "下架";
        } else if (productStatue == "下架") {
            productStatue = "上架";
        }
        let params = {
            productStatue: productStatue,
            productId: productId
        }
        let result = await ajaxGet("/product/updateProductStatue", params);
        if (result == "ok") {
            await showProduct();
        } else {
            alert("服务器错误");
        }
    }

    //修改商品单价
    async function updateProductPrice() {
        let productPrice = $("productPrice").value;
        let params = {
            productPrice: productPrice,
            productId: productId
        }
        let info = await ajaxGet("/product/updateProductPrice", params);
        if (info == "ok") {
            await showProduct();
        }
    }


</script>


<body>
<div id="productContent" style="display: flex;justify-content: center">

</div>
</body>
</html>